import React from 'react'
import {Row, Col, Button, Table, Input, Tag, Avatar, Modal, message} from 'antd'
// import { UserOutlined } from '@ant-design/icons';
import '../styles.css'
import Model from "./Model"
import axios from "axios";
import api from "../../../api/api";
import Addcompany from "../../Organization/CompanyManagement";

const {Search} = Input;


class PermissionSettings extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            title: "新增权限组",
            data: [],
            visible: false,
            pageNum: 1,
            pageSize: 10,
            total: 0
        };
    }

    componentDidMount() {
        console.log("赋值1111111111111");
        this.getPermissionSettings();
    }


    columns = [
        {
            title: 'ID',
            dataIndex: 'id',
            className: "elementHidden",
            key: 'id',
            width: '20%'
        },
        {
            title: '权限组名称',
            dataIndex: 'name',
            key: 'name',
            width: '20%'
        },
        {
            title: '更新时间',
            dataIndex: 'time',
            width: '20%',
            key: 'time'
        },
        {
            title: '操作',
            dataIndex: 'option',
            width: '40%',
            key: 'option',
            render: (text, record, index) => {
                return (
                    <span>
            <a className="optionMargin" onClick={() => this.onClickEdit(record)}>编辑</a>

            <a className="optionMargin" onClick={() => this.onClickDelete(record)}>删除</a>
          </span>
                )
            }
        }
    ];

    getPermissionSettings() {
        var that = this;
        axios.post(api.api_URL + "/admin/authority/queryDirectoryInfoByPage?pageNum=" + this.state.pageNum + "&pageSize=" + this.state.pageSize + "&orgCode=" + localStorage.getItem('orgCode'), {
            "name": this.state.inputValue,
            "orgCode": localStorage.getItem('orgCode')
        }, {
            headers: {
                "Authorization": localStorage.getItem('token'),
                "Content-Type": api.ContentType
            }
        }).then(function (res) {
            console.log(res.data)
            if (res.data.code != 1) {
                alert(res.data.msg);
                return;
            }
            let totalSize = res.data.data.list.totalSize;
            const dataList = [];
            for (let i = 0; i < res.data.data.list.content.length; i++) {
                let resContent = res.data.data.list.content[i];
                const content = {};
                content.key = (i + 1);
                content.id = resContent.authorityId;
                content.name = resContent.authorityName;
                content.time = resContent.createTime;
                dataList[i] = content;
            }
            that.setState({
                data: dataList,
                total: totalSize
            })
        }).catch(function (err) {
            message.error({content:"加载失败，请重新尝试！"})
            console.log(err);
        });
    }

//分页
    onBaseClick(current, pageSize) {
        this.setState({
            pageNum: current,
            pageSize: pageSize
        }, () => {
            this.getPermissionSettings()
        });

    }

    onclickAdd() {
        console.info("新增权限组");
        this.setState({
            title: "新增权限组",
            visible: true
        })
        console.info("this.state====", this.state);
    }

    //编辑
    onClickEdit(item) {
        console.info("编辑");
        this.setState({
            title: "编辑",
            visible: true
        })
        this.child.getPermissionSetting(item.id)
    }

    //删除
    onClickDelete(item) {
        let that = this;
        let configModel = Modal.confirm({
            title: '删除',
            content: '是否确认删除？',
            onOk: (values) => {
                return new Promise((res, rej) => {
                    let url = api.api_URL + "/admin/authority/deleteAuthorityInfo?authorityInfoId=" + item.id + "&orgCode=" + localStorage.getItem('orgCode');
                    axios.get(url, {
                        headers: {
                            "Authorization": localStorage.getItem('token'),
                            "Content-Type": api.ContentType
                        }
                    }).then(function (res) {
                        if (res.data.code != 1) {
                            message.error({content:"删除失败，请重新尝试！"})
                            return;
                        }
                        message.success({content:"删除成功！"})
                        that.setState({
                            data: []
                        })
                        that.getPermissionSettings();
                        configModel.destroy();
                    }).catch(function (err) {
                        message.error({content:"删除失败，请重新尝试！"})
                        console.log(err);
                    })
                }).catch((e) => console.warn(e));

            },
            onCancel() {
            },
        })


    }

    onRef = (ref) => {
        this.child = ref
    }

    handleCancel = e => {
        console.log(e);
        this.setState({
            visible: false,
        });
        this.getPermissionSettings();
    };

    render() {

        return (
            <div className="girdBg">
                <Row className="searchBox">
                    <Col span={20}>
                    </Col>
                    <Col span={4} className="btnRight">
                        <Button icon={"save"} type='primary' style={{marginLeft: 30}}
                                onClick={this.onclickAdd.bind(this)}>新增权限组</Button>
                    </Col>
                </Row>
                <Row style={{margin: 30}}>
                    <Table
                        columns={this.columns}
                        dataSource={this.state.data}
                        scroll={{x: 'max-content'}} // 加上这条  横向滚动  支持此属性的浏览器内容就不会换行了
                        columns={this.columns.map(item => { // 通过配置 给每个单元格添加不换行属性
                            const fun = () => ({style: {whiteSpace: 'nowrap'}});
                            item.onHeaderCell = fun;
                            item.onCell = fun;
                            return item;
                        })}
                        pagination={{
                            simple: false,
                            current: this.state.pageNum,
                            total: this.state.total,
                            pageSize: this.state.pageSize,
                            showTotal: (count = this.state.total) => {
                                return '共' + count + '条'
                            },
                            onChange: (current, pageSize) => {
                                this.onBaseClick(current, pageSize)
                            }
                        }}
                    />
                </Row>
                <Model data={this.state} handleCancel={this.handleCancel} onRef={this.onRef}/>

            </div>
        )
    }
}

export default PermissionSettings
